#include "UIControlThemeInterpretor.h"

IUITheme* UIControlThemeInterpretor::iTheme = NULL;

UIControlThemeInterpretor::UIControlThemeInterpretor()
{
}

UIControlThemeInterpretor::~UIControlThemeInterpretor()
{
}

void UIControlThemeInterpretor::DrawBorder(CGraphicsContext& aGc,IUIControl* pControl)
{
	const TRect rcBorder = pControl->GetBorderRect();
	const TRect rcPadding = pControl->GetPaddingRect();
	
	TSize lineSize;
	TRgb lineColor;
	CGraphicsContext::TPenStyle lineStyle;
	
	TPoint ptBegin;
	TPoint ptEnd;
	
	UIStyle& style = pControl->GetUIStyle();

	
	//左border
	if(style.iBorder.IsSet(UIBorder::EBS_SetLeft))
		{
		ptBegin.iX = rcBorder.iTl.iX + (rcPadding.iTl.iX - rcBorder.iTl.iX)/2;
		ptBegin.iY = rcBorder.iTl.iY;
		ptEnd.iX = ptBegin.iX;
		ptEnd.iY = rcBorder.iBr.iY;
		
		lineSize.SetSize(rcPadding.iTl.iX - rcBorder.iTl.iX,
						rcPadding.iTl.iY - rcBorder.iTl.iY);
		lineColor = style.iBorder.iBorderL.iColor.GetColor();
		lineStyle = ConvertPenStyle(style.iBorder.iBorderL.iBorderStyle);
		
		if(iTheme)
			iTheme->DrawLine(aGc,lineSize,lineColor,lineStyle,ptBegin,ptEnd);
		}
	

	//上border	
	if(style.iBorder.IsSet(UIBorder::EBS_SetTop))
		{
		ptBegin.iX = rcBorder.iTl.iX;
		ptBegin.iY = rcBorder.iTl.iY + (rcPadding.iTl.iY - rcBorder.iTl.iY)/2;
		ptEnd.iX = rcBorder.iBr.iX;
		ptEnd.iY = ptBegin.iY;
		
		lineSize.SetSize(rcPadding.iTl.iY - rcBorder.iTl.iY,
				rcPadding.iTl.iY - rcBorder.iTl.iY);
		lineColor = style.iBorder.iBorderT.iColor.GetColor();
		lineStyle = ConvertPenStyle(style.iBorder.iBorderT.iBorderStyle);
		
		if(iTheme)
			iTheme->DrawLine(aGc,lineSize,lineColor,lineStyle,ptBegin,ptEnd);
		}
	
	//右border
	if(style.iBorder.IsSet(UIBorder::EBS_SetRight))
		{
		ptBegin.iX = rcBorder.iBr.iX - (rcBorder.iBr.iX - rcPadding.iBr.iX)/2;
		ptBegin.iY = rcBorder.iTl.iY;
		ptEnd.iX = ptBegin.iX;
		ptEnd.iY = rcBorder.iBr.iY;
		
		lineSize.SetSize(rcBorder.iBr.iX - rcPadding.iBr.iX,
				rcBorder.iBr.iX - rcPadding.iBr.iX);
		lineColor = style.iBorder.iBorderR.iColor.GetColor();
		lineStyle = ConvertPenStyle(style.iBorder.iBorderR.iBorderStyle);
		
		if(iTheme)
			iTheme->DrawLine(aGc,lineSize,lineColor,lineStyle,ptBegin,ptEnd);
		}
	
	//下border
	if(style.iBorder.IsSet(UIBorder::EBS_SetBottom))
		{
		ptBegin.iX = rcBorder.iTl.iX;
		ptBegin.iY = rcBorder.iBr.iY - (rcBorder.iBr.iY - rcPadding.iBr.iY)/2;
		ptEnd.iX = rcBorder.iBr.iX;
		ptEnd.iY = ptBegin.iY;
		
		lineSize.SetSize(rcBorder.iBr.iY - rcPadding.iBr.iY,
				rcBorder.iBr.iY - rcPadding.iBr.iY);
		lineColor = style.iBorder.iBorderB.iColor.GetColor();
		lineStyle = ConvertPenStyle(style.iBorder.iBorderB.iBorderStyle);
		
		if(iTheme)
			iTheme->DrawLine(aGc,lineSize,lineColor,lineStyle,ptBegin,ptEnd);
		}
}

CGraphicsContext::TPenStyle UIControlThemeInterpretor::ConvertPenStyle(UISingleBorder::EUIBorderStyle style)
{
	switch(style)
	{
	case UISingleBorder::EBStyle_Solid:
		return CGraphicsContext::ESolidPen;
	case UISingleBorder::EBStyle_Dash:
		return CGraphicsContext::EDashedPen;
	case UISingleBorder::EBStyle_Dot:
		return CGraphicsContext::EDottedPen;
	default:
		return CGraphicsContext::ENullPen;
	}
}